import time
from selenium.webdriver.support import expected_conditions as EC

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

# 设置Edge WebDriver路径
edge_driver_path = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedgedriver.exe'

# 初始化WebDriver
service = Service(edge_driver_path)
options = webdriver.EdgeOptions()
options.add_argument("--remote-debugging-port=9225")  # 配置端口，连接到已登录的Edge
driver = webdriver.Edge(service=service, options=options)

# 设置浏览器窗口宽度和高度
width = 1520  # 可根据需要调整宽度
height = 880  # 可根据需要调整高度
driver.set_window_size(width, height)

# 要注入 Cookie 的目标网站
target_url = 'https://www.jd.com'
driver.get(target_url)

# 从 cookie.txt 文件中读取 Cookie 并注入到浏览器
try:
    with open('1-ck.txt', 'r', encoding='utf-8') as file:
        cookie_str = file.read().strip()
        cookies = cookie_str.split('; ')
        for cookie in cookies:
            if '=' in cookie:
                name, value = cookie.split('=', 1)
                cookie_dict = {
                    'name': name,
                    'value': value,
                    'domain': '.jd.com'  # 替换为实际的域名，需要包含域名前缀，如 .example.com
                }
                driver.add_cookie(cookie_dict)
    print("Cookie 注入成功")
except Exception as e:
    print(f"注入 Cookie 时出现问题: {e}")


# 重新加载页面以使 Cookie 生效
driver.refresh()

# 打开目标网页
url = 'https://mall.jd.com/view_search-4244265-15523024-14225663-0-0-0-0-1-1-60.html?isGlobalSearch=0&other='
driver.get(url)

# 延时等待页面加载
time.sleep(1)

# 翻页并抓取数据
page_num = 1
while True:
    # 滚动页面直到加载完商品
    page_area = driver.find_element(By.CSS_SELECTOR, '.jPage')
    driver.execute_script("arguments[0].scrollIntoView();", page_area)
    time.sleep(1)

    # 获取商品列表
    items = driver.find_elements(By.CSS_SELECTOR, '.jSearchListArea .jSubObject')

    # 打开文件进行保存
    with open(f'page_{page_num}.txt', 'w', encoding='utf-8') as file:
        for item in items:
            try:
                # 获取商品名称、价格和链接
                product_name = item.find_element(By.CSS_SELECTOR, '.jDesc a').text
                product_price = item.find_element(By.CSS_SELECTOR, '.jdPrice .jdNum').text
                product_link = item.find_element(By.CSS_SELECTOR, '.jDesc a').get_attribute('href')

                # 写入文件
                file.write(f'{product_name}|{product_price}|{product_link}\n')
            except Exception as e:
                print(f"错误: {e}")

    print(f'保存第 {page_num} 页数据完毕')

    # 尝试点击“下一页”按钮
    # 确保页面加载完成
    try:
        # 显式等待“下一页”按钮可点击
        next_page = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.LINK_TEXT, '下一页'))
        )
        next_page.click()
        print("已成功点击“下一页”按钮")
        page_num += 1
        time.sleep(1)  # 等待页面加载
    except Exception as e:
        print(f"出现问题：{e}")

# 最后滚动到“jAdvanced”元素
advanced_area = driver.find_element(By.CSS_SELECTOR, '.jAdvanced')
driver.execute_script("arguments[0].scrollIntoView();", advanced_area)
time.sleep(1)

# 关闭浏览器
driver.quit()
